package logic

import (
	"context"
	"fmt"
	"good_srv/model/mysql"
	"good_srv/model/redis"

	"good_srv/good"
	"good_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *good.LoginRequest) (*good.LoginResponse, error) {
	// todo: add your logic here and delete this line
	sms := redis.Client.Get(context.Background(), "SendSmsLogin"+in.Phone)
	if sms.Val() != in.SendSms {
		return nil, fmt.Errorf("验证码输入错误")
	}
	var user mysql.User
	err := user.FindUser(in.Phone)
	if err != nil {
		return nil, fmt.Errorf("该用户不存在")
	}
	return &good.LoginResponse{
		Id: int64(user.ID),
	}, nil
}
